using AutoMapper;
using FastDevTemplate.DataAccess.Entities;
using FastDevTemplate.Service.Interface.DTO;

namespace FastDevTemplate.Service.DataMapper;

public class DtoMapProfile : Profile
{
    public DtoMapProfile()
    {
        //Enter Config:
        CreateMap<Role, RoleResultDto>();
        CreateMap<Role, RoleGetResultDto>()
            .ForMember(dest => dest.PermissionIDs,
                opt => opt.MapFrom(
                    src => src.RolePermissions.Select(item => item.PermissionID))
            );
        CreateMap<RoleAddDto, Role>();
        CreateMap<RoleModifyDto, Role>();

        CreateMap<UserRole, UserLoginRoleInfoDto>()
            .ForMember(dest => dest.PermissionIDs,
                opt => opt.MapFrom(
                    src => src.Role.RolePermissions.Select(item => item.PermissionID))
            );

        CreateMap<User, UserResultDto>();
        CreateMap<User, UserGetResultDto>()
            .ForMember(dest => dest.RoleIDs,
                opt => opt.MapFrom(
                    src => src.UserRoles.Select(item => item.RoleID))
            );

        CreateMap<User, UserLoginDto>();
        CreateMap<UserUpdateDto, User>()
            .ForAllMembers(opt =>
                opt.Condition((src, dest, srcMember) => srcMember != null));
        CreateMap<UserAddDto, User>();
        CreateMap<User, UserUpdateDto>();
        
        CreateMap<Permission, PermissionResultDto>();
        CreateMap<PermissionAddDto, Permission>();
        CreateMap<PermissionModifyDto, Permission>();
        
        CreateMap<LogAddDto, Log>();
        CreateMap<Log, LogResultDto>();
        
    }
}